package client.net.handler;

import client.Client;
import java.nio.ByteBuffer;
import shared.util.PacketIO;

/**
 * 
 * This class handles updates from the server for a game
 * the client is observing
 *
 * @author Vincent Tsuei
 * @version 2012-11-29
 */
public class ObserveGameUpdateHandler implements ClientPacketHandler {

    @Override
    public int processPacket(Client client, ByteBuffer packet) {
        if (!client.isObserving()) {
            return -1; //Ignore if not observing
        }
        
        long gameID = packet.getLong();
        String p1 = PacketIO.readString(packet);
        String p2 = PacketIO.readString(packet);
        String pturn = PacketIO.readString(packet);
        int[] set = new int[packet.getInt()];
        for (int i = 0;i < set.length;i++) {
            set[i] = packet.getInt();
        }
        
        client.getUserIO().write("", true);
        client.getUserIO().write("Game " + gameID + " | [" + p1 + "] vs. [" + p2 + "]", true);
        client.getUserIO().write("It is currently " + pturn + "'s turn:\t", false);
        for (int i : set) {
            client.getUserIO().write(i + " ", false);
        }
        client.getUserIO().write("", true);
        
        
        
        byte gameOver = packet.get();
        if (gameOver == 1) {
            String winner = PacketIO.readString(packet);
            client.getUserIO().write("The game is over! " + winner + " has won.", true);
        }
        
        return 0;
    }
    
}
